package StackSecond.SwordOffer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FindMinDifference {
    /**
     * 给定一个 24 小时制（小时:分钟 "HH:MM"）的时间列表，找出列表中任意两个时间的最小时间差并以分钟数表示。
     * <p>
     * 这里要计算当前时间和当前时间后一天这个时间点的的时间，主要是为了防止跨夜计算的情况，比如题目里的输入：timePoints = ["23:59","00:00"],输出：1
     */
    public static int findMinDifference(List<String> timePoints) {
        int size = timePoints.size();
        int[] arrays = new int[size * 2];
        for (int i = 0, j = 0; i < size; j += 2, i++) {
            String[] split = timePoints.get(i).split(":");
            int h = Integer.parseInt(split[0]);
            int m = Integer.parseInt(split[1]);
            int seconds = h * 60 + m;

            arrays[j] = seconds;
            arrays[j + 1] = seconds + 60 * 24;
        }
        Arrays.sort(arrays);
        int ans = Integer.MAX_VALUE;
        for (int i = 0; i < arrays.length - 1; i++) {
            ans = Math.min(ans, arrays[i + 1] - arrays[i]);
        }
        return ans;
    }

    public static void main(String[] args) {
        List<String> timePoints = new ArrayList<>();
        timePoints.add("23:59");
        timePoints.add("00:00");
        int minDifference = findMinDifference(timePoints);
        System.out.println(minDifference);
    }
}
